package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.integral.IntegralDetailDto;
import com.java110.intf.acct.IIntegralDetailV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("queryCommunityIntegralDetailBmoImpl")
public class QueryCommunityIntegralDetailBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IIntegralDetailV1InnerServiceSMO integralDetailV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区");
        Assert.hasKeyAndValue(reqJson, "integralId", "未包含积分ID");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        IntegralDetailDto integralDetailDto = BeanConvertUtil.covertBean(reqJson, IntegralDetailDto.class);

        int count = integralDetailV1InnerServiceSMOImpl.queryIntegralDetailsCount(integralDetailDto);

        List<IntegralDetailDto> integralDetailDtos = null;

        if (count > 0) {
            integralDetailDtos = integralDetailV1InnerServiceSMOImpl.queryIntegralDetails(integralDetailDto);
        } else {
            integralDetailDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, integralDetailDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);

    }
}
